package client;

import codec.FileCodecFactory;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import java.io.File;
import java.net.InetSocketAddress;

/**
 * Created by ryan on 15-8-25.
 */
public class FileClient{

    private String fileName;
    private long startPos;
    private int length;
    private String ip;
    private int port;

    public static long startTime;
    public static int transmitSpeed;

    public FileClient(String fn, long sp, int ln, String ip, int port) {
        fileName = fn;
        startPos = sp;
        length = ln;
        this.ip = ip;
        this.port = port;
    }

    public void createSession() {
        try {
            IoConnector connector = new NioSocketConnector();
            connector.setConnectTimeoutMillis(30000);
            connector.getFilterChain().addLast("codec",
                    new ProtocolCodecFilter(new FileCodecFactory(true)));
            connector.setHandler(new FileClientIoHandler(fileName, startPos, length));
            ConnectFuture future = connector.connect(new InetSocketAddress(ip, port));
            future.awaitUninterruptibly();
            IoSession session = null;
            try {
                session = future.getSession();
            } catch (Exception e) {
                e.printStackTrace();
            }
            //return session;
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    public static void send(String[] args) {

        String fileName = "/home/ryan/temp/test.cpp";
        String ip = "127.0.0.1";
        int clientNum = 4;
        int port = 9121;
        transmitSpeed = 1024 * 1024 * 2;

        if (args.length >= 4) {
            fileName = args[0];
            ip = args[1];
            clientNum = Integer.parseInt(args[3]);
            port = Integer.parseInt(args[2]);
            if (args.length >= 5)
                transmitSpeed = Integer.parseInt(args[4]);
            System.out.println("fileName\t: " + fileName);
            System.out.println("ip\t: " + ip);
            System.out.println("clientNum\t: " + clientNum);
            System.out.println("port\t: " + port);
            System.out.println("transmitSpeed\t: " + transmitSpeed);
        }
        else {
            System.out.println("fileName ip port threadNum transmitSpeed");
            //System.exit(1);
        }

        long fileLength = (new File(fileName)).length();
        int blockSize = (int) (fileLength / clientNum);
        long startPos = 0;

        startTime = System.nanoTime();

        for (int i=0; i!= clientNum; i++) {
            FileClient client = new FileClient(fileName, startPos, blockSize, ip, port);
            //System.out.println("" + startPos + " " + blockSize);
            client.createSession();
            startPos += blockSize;
        }
        // remaining...
        if ((fileLength - startPos) != 0) {
            FileClient client = new FileClient(fileName, startPos, (int)(fileLength-startPos), ip, port);
            System.out.println("" + startPos + " " + (fileLength-startPos));
            client.createSession();
        }
    }
}
